package com.hudongwx.godoffood.activity.home_activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.LoginActivity;
import com.hudongwx.godoffood.activity.myActivity.CommunityMsgActivity;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment1;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment2;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment3;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment4;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment5;
import com.hudongwx.godoffood.fragment.FoodMenuFragment.Fragment6;
import com.hudongwx.godoffood.utils.MyApplication;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_food_classification)
public class FoodClassificationActivity extends AppCompatActivity {
    @ViewInject(R.id.food_tablayout)
    TabLayout tabLayout;
    @ViewInject(R.id.food_view_pager)
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //确定主页点击的分类
        int type = getIntent().getIntExtra("type",100);
        viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
        //根据主页点击的分类直接显示相对应的页面
        switch (type){
            case 0:
                viewPager.setCurrentItem(type);
                break;
            case 1:
                viewPager.setCurrentItem(type);
                break;
            case 2:
                viewPager.setCurrentItem(type);
                break;
            case 3:
                viewPager.setCurrentItem(type);
                break;
            case 4:
                viewPager.setCurrentItem(type);
                break;
            case 5:
                viewPager.setCurrentItem(type);
                break;
        }
        //关联tablayout和viewpager
        tabLayout.setupWithViewPager(viewPager);
    }

    class PageAdapter extends FragmentPagerAdapter{
        Fragment[] fragments = new Fragment[6];
        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    fragments[position] = new Fragment1();
                    break;
                case 1:
                    fragments[position] = new Fragment2();
                    break;
                case 2:
                    fragments[position] = new Fragment3();
                    break;
                case 3:
                    fragments[position] = new Fragment4();
                    break;
                case 4:
                    fragments[position] = new Fragment5();
                    break;
                case 5:
                    fragments[position] = new Fragment6();
                    break;
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String [] str = new String[6];
            switch (position){
                case 0:
                    str[position]="好油";
                    break;
                case 1:
                    str[position]="好面";
                    break;
                case 2:
                    str[position]="好果";
                    break;
                case 3:
                    str[position]="好浆";
                    break;
                case 4:
                    str[position]="好酒";
                    break;
                case 5:
                    str[position]="好酱";
                    break;
            }
            return str[position];
        }
    }


    //返回
    @Event(R.id.food_content_back)
    private void back(View view){
        super.onBackPressed();
    }
    //跳转社区
    @Event(R.id.food_community)
    private void toCommunity(View view){
        String uid = MyApplication.getStorage("Uid");
        if (!uid.equals("1")){
            startActivity(new Intent(this, LoginActivity.class));
        }else {
            startActivity(new Intent(this, CommunityMsgActivity.class));
        }

    }
    //跳转搜索
    @Event(R.id.tosearch)
    private void toSearch(View view){
        Intent intent = new Intent(this, SearchActivity.class);
        startActivity(intent);
    }
}
